﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using SheepStorage.API.Application.Command.EnterpriseCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;
using System.Collections.Generic;

namespace SheepStorage.API.Controllers
{
    /// <summary>
    /// 企业管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class EnterpriseController : ControllerBase
    {

        private readonly IMediator _mediator;
      

        public EnterpriseController(IMediator mediator)
        {
            _mediator = mediator;
          
        }

        /// <summary>
        /// 企业添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> Create([FromForm]EnterpriseCreateCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 企业显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<PageOutput<Enterprise>>> GetEnterprises(EnterpriseListCommand command)
        {
            return await _mediator.Send(command);
        }
        
        /// <summary>
        /// 删除企业
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> Del(EnterpriseDelCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<string>> Upload(IFormFile file)
        {
            string fileName = file.FileName;
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(fileName).ToLower();
            string path = Directory.GetCurrentDirectory() + @"\wwwroot\Image\" + fileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            string returnurl = "/Image/" + fileName;
            return new Results<string>
            {
                Code = 200,
                Msg = returnurl
            };
        }
       
    }
}
